
//
//  OHA_SimplyAFNRequest.m
//  OneHourArrvie
//
//  Created by Sunny on 16/12/13.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "base_SimplyAFNRequest.h"



@implementation base_SimplyAFNRequest{
    dispatch_group_t group;
    dispatch_queue_t q;
   
     }
//-(instancetype)initWithRequestByURL:(NSString *)url WithDictionary:(NSDictionary *)dictionary WithRequestModel:(id)model WithResults:(NSString *)results WithBlock:(PostRequestBlock)block{
//    self = [super init];
//    if (self) {
//        [self getDataWithParams:dictionary WithRequestURL:url];
//    }
//    return self;
//}
//

+(void)getDataWithParams:(NSDictionary *)dict WithRequestURL:(NSString *)url WithBlock:(PostRequestBlock)successBlock withFailedBlock:(RequestFailedBlock)failedBlock{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置证书模式，AFSSLPinningModeNone，代表前端包内不验证
    //在单向认证时，前端不放证书，服务器去验证
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    // 如果是需要服务端验证证书，需要设置为YES
    securityPolicy.allowInvalidCertificates = YES;
    //validatesDomainName 是否需要验证域名，默认为YES；
    securityPolicy.validatesDomainName = NO;
    //设置验证模式
    manager.securityPolicy = securityPolicy;
    [manager POST:url parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] isEqualToString:@"100"]) {
            if (successBlock) {
                successBlock(responseObject);
            }
        }
        else{
            if (failedBlock) {
                failedBlock(responseObject);
            }
             [base_Tooltip SVProgressShowFailMessage:responseObject[@"message"]];
          
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败");
        NSLog(@"%@",task);
        NSLog(@"%@",error);
        if (failedBlock) {
            failedBlock(error);
        }
    }];

    
}


+(void)uploadImageView:(NSDictionary *)dict WithRequestURL:(NSString *)url WithImageData:(NSData *)imageData WithImageName:(NSString *)imageName WithFileName:(NSString *)fileName WithImageDictionary:(NSMutableDictionary *)imageDictionary WithSuccessBlock:(uploadImageBlock)successBlock {
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (int a = 0; a < imageDictionary.count; a ++) {
            [formData appendPartWithFileData:imageData name:imageName fileName:fileName mimeType:@"image/png"];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (successBlock) {
            successBlock();
        }
        else{
            [base_Tooltip SVProgressShowFailMessage:responseObject[@"message"]];
 
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败");
    }];
}



-(instancetype)init{
    group = dispatch_group_create();
    q = dispatch_get_global_queue(0, 0);
    return self;
}


-(void)addQueneToGroupWithRequestURL:(NSString *)url WithParams:(NSDictionary *)dict
                           WithBlock:(PostRequestBlock)successBlock withFailedBlock:(RequestFailedBlock)failedBlock{
    dispatch_group_async(group, q, ^{
        dispatch_group_enter(group);//很重要,不能少
        NSLog(@"队列A启动");
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        //设置证书模式，AFSSLPinningModeNone，代表前端包内不验证
        //在单向认证时，前端不放证书，服务器去验证
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        // 如果是需要服务端验证证书，需要设置为YES
        securityPolicy.allowInvalidCertificates = YES;
        //validatesDomainName 是否需要验证域名，默认为YES；
        securityPolicy.validatesDomainName = NO;
        //设置验证模式
        manager.securityPolicy = securityPolicy;
        [manager POST:url parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"%@",responseObject);
            if ([responseObject[@"code"] isEqualToString:@"100"]) {
                NSLog(@"队列A结束");
               
                if (successBlock) {
                    successBlock(responseObject);
                }
            }
            else{
                if (failedBlock) {
                    failedBlock(responseObject);
                }
            }
            dispatch_group_leave(group);//很重要,不能少
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"网络请求失败");
            dispatch_group_leave(group);//很重要,不能少
        }];
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        // 修改为主队列，后台批量下载，结束后，主线程统一更新UI
        NSLog(@"OK 都所有队列完毕 %@", [NSThread currentThread]);
        [_delegate reloadAllData];
    });
   
}


@end
